<?php
/**
 * 刷数据
 * User: 13651
 * Time: 2025/10/16 16:13
 */

namespace App\Services;

use App\Extensions\BaseService;
use App\Models\Bi\RefundLog;
use App\Models\Order;
use App\Models\RefundOrder;
use App\Models\Store;
use App\Param\System\SystemDataSaveParam;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;

class SystemDataSaveService  extends BaseService
{

    public function updateData(SystemDataSaveParam $params)
    {
        $storeId = $params->getStoreId();
        $startTime = $params->getStartTime();
        $endTime = $params->getEndTime();

        $tableArr = [
            'bill_logs',
            'bills',
            'bill_logs_'.date('Ym',strtotime($startTime)),
            'bills_'.date('Y',strtotime($startTime)),
        ];

        foreach ($tableArr as $key=>&$table) {
            if (!Schema::connection("bi")->hasTable($table)) {
                return ['status'=>2,'message'=>$table.'表信息不存在!'];
            }
        }

        $prefix = "bi_";
        $query = DB::connection("bi");
        $query->getPdo()->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
        $bi_bill_logs = $query->table('bill_logs', true, $prefix);
        $bi_bill = $query->table('bills', true, $prefix);
        $bill_logs_date = $query->table('bill_logs_'.date('Ym',strtotime($startTime)), true, $prefix);
        $bi_bills_date = $query->table('bills_'.date('Y',strtotime($startTime)), true, $prefix);


        foreach ($storeId as $store_id) {
            $check = Store::query()->select(["store_id"])->where('store_id', $store_id)->first();
            Log::info("系统刷数据查询门店ID：$store_id-返回信息".$check);
            if (empty($check))
                continue;

            /**
             * 业务数据查询
             */
            $yields = Order::query()
                ->where("created_at", ">=", $startTime)
                ->where("created_at", "<=", $endTime)
                ->where("store_id", $store_id)
                ->whereIn("pay_status", [1, 6])
                ->orderBy("id", "asc")
                ->get();
            if ($yields->isEmpty())
                continue;
            Log::info("系统刷数据查询门店ID：$store_id-订单信息",$yields->toArray());
            /**
             * 清空对账数据
             */
            $bi_bill_logs->where("day", ">=", $startTime)
                ->where("day", "<=", $endTime)
                ->where("store_id", $store_id)
                ->delete();
            $bill_logs_date->where("day", ">=", $startTime)
                ->where("day", "<=", $endTime)
                ->where("store_id", $store_id)
                ->delete();
            $bi_bill->where("day", ">=", $startTime)
                ->where("day", "<=", $endTime)
                ->where("store_id", $store_id)
                ->delete();
            $bi_bills_date->where("day", ">=", $startTime)
                ->where("day", "<=", $endTime)
                ->where("store_id", $store_id)
                ->delete();

            foreach ($yields as $k2 => $order) {
                /**
                 * 链接数据库
                 */
                $redis = Redis::connection('amqpQueue');
                $data = [
                    "out_trade_no" => $order->out_trade_no,
                    "type" => 1,
                    "from" => "pay",
                ];

                /**
                 * 队列投递数据
                 */
                $redis->rPush("payListener", json_encode($data));

                if ($order->pay_status == 6) {
                    RefundLog::query()->where("out_trade_no", $order->out_trade_no)->delete();
                    $refund = RefundOrder::query()->where("out_trade_no", $order->out_trade_no)->first();
                    $data = [
                        "out_trade_no" => $order->out_trade_no,
                        "refund_no" => empty($refund) ? "" : $refund->refund_no,
                        "type" => 2,
                        "from" => "pay",
                    ];
                    $redis->rPush("payListener", json_encode($data));
                }
            }
        }
        return ['status'=>1,'message'=>'执行成功'];
    }
}